//
//  JMBCouponAddBaseVC.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/8/8.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "JMBCouponAddBaseVC.h"
#import "JMBCouponAddChooseCell.h"
#import "JMBCouponAddInputCell.h"
#import "JMBCouponAddSelectCell.h"
#import "JMBCouponDatePickerView.h"
@interface JMBCouponAddBaseVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation JMBCouponAddBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _addUI];
    [self _setupNavigationItem];
}

- (void)_addUI{
    UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    [self addChildViewController:tableViewController];
    [tableViewController.view setFrame:CGRectMake(0,0, self.view.width, self.view.height)];
    
    // 获取tableViewController的tableView实现表单自动上移
    _formTableView = tableViewController.tableView;
    _formTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    _formTableView.dataSource = self;
    _formTableView.delegate = self;
    _formTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    _formTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _formTableView.backgroundColor = UIColorHex(F3F3F3);
    _formTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, CGFLOAT_MIN)];
    _formTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, CGFLOAT_MIN)];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_fingerTapped:)];
    singleTap.cancelsTouchesInView = NO;
    [_formTableView addGestureRecognizer:singleTap];
    
    [self.view addSubview:_formTableView];
}

#pragma mark -- TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.mutableItems.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSParameterAssert([self.mutableItems[section] isKindOfClass:[FormSectionItem class]]);
    FormSectionItem *sectionItem = self.mutableItems[section];
    return sectionItem.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FormSectionItem *sectionItem = self.mutableItems[indexPath.section];
    NSParameterAssert([sectionItem.items[indexPath.row] isKindOfClass:[CouponFormItem class]]);
    WeakSelf
    CouponFormItem *item = sectionItem.items[indexPath.row];
    if (item.itemType == CouponFormItemTypeNormal) {
        JMBCouponAddInputCell *cell = [JMBCouponAddInputCell dequeueCellWithTableView:tableView indexPath:indexPath];
        cell.item = item;
        cell.inputEndCompletion = ^(NSString *text) {
            StrongSelf
            [self updateInputWithText:text indexPath:indexPath];
        };
        return cell;
    }else if (item.itemType == CouponFormItemTypeTimeSelected){
        JMBCouponAddSelectCell *cell = [JMBCouponAddSelectCell dequeueCellWithTableView:tableView indexPath:indexPath];
        cell.item = item;
        return cell;
    }
    JMBCouponAddChooseCell *cell = [JMBCouponAddChooseCell dequeueCellWithTableView:tableView indexPath:indexPath];
    cell.item = item;
    cell.cellSelectedLeftButtonCompletion = ^(BOOL isSelectedLeftButton) {
        StrongSelf
        [self updateChooseCell:isSelectedLeftButton indexPath:indexPath];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != 4) {
        return;
    }
    JMBCouponDatePickerView *view = [JMBCouponDatePickerView viewFromXib];
//    [self.view addSubview:view];
    [view show];
//    [self updateInputWithText:@"" indexPath:indexPath];
}




#pragma mark -- 表单条目响应block处理
- (void)updateInputWithText:(NSString *)text indexPath:(NSIndexPath *)indexPath{
    FormSectionItem *sectionItem = self.mutableItems[indexPath.section];
    CouponFormItem *item = sectionItem.items[indexPath.row];
    item.info = text;
    [self.formTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)updateChooseCell:(BOOL )isSelectedLeftButton indexPath:(NSIndexPath *)indexPath{
    FormSectionItem *sectionItem = self.mutableItems[indexPath.section];
    CouponFormItem *item = sectionItem.items[indexPath.row];
    item.info = isSelectedLeftButton? @"0" : @"1";
    
    if (item.itemType == CouponFormItemTypeChooseCouponType) {
        [self changeCell:isSelectedLeftButton];
    }
    
    [self.formTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)changeCell:(BOOL)isCashCoupon{
    if (self.mutableItems.count > 0) {
        FormSectionItem *sectionItem = self.mutableItems[0];
        NSMutableArray *tempArray = [NSMutableArray arrayWithArray:sectionItem.items];
        CouponFormItem *replaceItem = [FormItemMaker replaceItemIsCash:isCashCoupon];
        [tempArray replaceObjectAtIndex:1 withObject:replaceItem];
        FormSectionItem *tempSectionItem =  SectionItem(tempArray);
        [self.mutableItems removeAllObjects];
        [self.mutableItems addObject:tempSectionItem];
        [self.formTableView reloadData];
    }
}

#pragma mark -- TableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    FormSectionItem *sectionItem = self.mutableItems[indexPath.section];
    CouponFormItem *item = sectionItem.items[indexPath.row];
    return item.defaultHeight;
//        return [FormNormalCell heightWithItem:item];
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    FormSectionItem *sectionItem = self.mutableItems[section];
    return sectionItem.headerHeight > 0 ? sectionItem.headerHeight:0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    FormSectionItem *sectionItem = self.mutableItems[section];
    return sectionItem.footerHeight > 0 ? sectionItem.footerHeight:0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    FormSectionItem *sectionItem = self.mutableItems[section];
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, sectionItem.headerHeight)];
    return sectionItem.headerView ? sectionItem.headerView:header;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    FormSectionItem *sectionItem = self.mutableItems[section];
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, sectionItem.footerHeight)];
    return sectionItem.footerView ? sectionItem.footerView:footer;
}

#pragma mark - 设置导航栏
- (void)_setupNavigationItem{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(_shuffle:)];
}
#pragma mark - 事件处理Or辅助方法
- (void)_shuffle:(UIBarButtonItem *)sender{
    [FormHandle jm_checkFormNullDataWithWithDatas:self.mutableItems success:^{
    
    } failure:^(NSString *error) {
        [MBProgressHUD showAutoMessage:error ToView:self.view];
    }];
}

- (void)_fingerTapped:(UITapGestureRecognizer *)gestureRecognizer {
    [self.view endEditing:YES];
}
- (NSMutableArray *)mutableItems {
    if (!_mutableItems) {
        _mutableItems = [[NSMutableArray alloc]init];
    }
    return _mutableItems;
}


@end
